import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_modular/flutter_modular.dart';
import 'package:thinking/config/_.dart';
import 'package:thinking/entity/_.dart';
import 'package:thinking/service/_.dart';
import 'package:thinking/utils/_.dart';
import 'package:thinking/widget/_.dart';

class SearchUserValue extends StatefulWidget{
  final String value;
  SearchUserValue({required this.value});
  @override
  _SearchUserValue createState() => _SearchUserValue(value: value);

}


class _SearchUserValue extends State<SearchUserValue> with AutomaticKeepAliveClientMixin<SearchUserValue>{
  final String value;
  List<UserData> userData = [];
  late  PageEntity _pageEntity;
  late EasyRefreshController _easyRefreshController;
  late Future<dynamic> _function;
  _SearchUserValue({required this.value});

  @override
  bool get wantKeepAlive => true;

  @override
  void initState() {
    super.initState();
    _pageEntity = PageEntity();
    _easyRefreshController = EasyRefreshController();
    _function = future();
  }
  future() async {
    await getValue();
  }
  Future<void> getValue() async {
    var res =  (await SearchService.getSearchUserData(params: {
      "search_Value":value,
      "search_Page":_pageEntity.page,
      "search_count":_pageEntity.count,
      "user_Id": SPreferences().getInt('user_Id')
    }))!;
    if (!_pageEntity.canLoad) {
      BotToast.showText(text: '没有更多用户了哦~');
    } else {
      BotToast.showText(text: '更新了${res.length}条用户~');
    }
    if (res.length < _pageEntity.count && _pageEntity.canLoad) {
      _pageEntity.setPage = _pageEntity.page + 1;
      _pageEntity.setCanLoad = false;
    }
    userData = [...userData,...res];
    setState(() {

    });



  }

  @override
  void dispose() {
    _easyRefreshController.dispose();
    super.dispose();
  }

  Widget easyRefresh(Widget child) {
    return EasyRefresh(
      child: child,
      header: BezierCircleHeader(backgroundColor: ColorConfig.ThemeColor),
      footer: BezierBounceFooter(backgroundColor: ColorConfig.ThemeColor),
      enableControlFinishRefresh: false,
      enableControlFinishLoad: true,
      controller: _easyRefreshController,
      bottomBouncing: true,
      onLoad: () async {
        if (_pageEntity.canLoad){
          _pageEntity.setPage = _pageEntity.page + 1;
        }

        await getValue();
        _easyRefreshController.finishLoad(success: true);
      },
    );
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return FutureBuilder(
        future: _function,
        builder: (BuildContext context, AsyncSnapshot snapshot){
          return snapshot.connectionState == ConnectionState.done?
          easyRefresh(
              Empty(child: ListView(
                shrinkWrap: true,
                physics: BouncingScrollPhysics(),
                children:userData.map((e) => GestureDetector(
                  onTap: (){
                    // Modular.
                  },
                  child: GestureDetector(
                    onTap: (){
                      Modular.to.pushNamed('/personal/${e.userId}');
                    },
                    child: UserItemWidget(userData: e,showButton: e.userId != SPreferences().getInt('user_Id'),),
                  ),
                )).toList(),
              ),isShow: userData.length == 0,)
          )
              :Loading(height: 300,);
        });
  }

}